<template>
  <div class="food-management-container">
    
        <el-form :inline="true" :model="queryForm" style="text-align: left; position: relative;">
          <el-form-item label="餐品名称">
            <el-input    v-model="queryForm.mname"   placeholder="请输入餐品名称" clearable  style="width: 220px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getTableData()">查询</el-button>
          </el-form-item>

          <el-form-item style="float: right;">
            <el-button type="primary" @click="handleAdd">新增</el-button>
            <el-button type="primary" @click="handleEdit">修改</el-button>
            <el-button type="danger" @click="handleDelete">删除</el-button>
          </el-form-item>
        </el-form>

  
        <el-table 
          ref="multipleTable" 
          :data="mealList" 
          tooltip-effect="dark" 
          style="width: 100%" 
          height="430px"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column label="序号" width="100" align="center">
            <template slot-scope="scope">
              {{ (queryForm.page - 1) * queryForm.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="餐品编号" prop="mid" width="150" align="center"></el-table-column>
          <el-table-column label="餐品名称" prop="mname" min-width="250" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="单价" prop="price" width="120" align="center">
            <template slot-scope="scope">¥{{ scope.row.price }}</template>
          </el-table-column>
          <!-- 减小空列的宽度 -->
          <el-table-column label="" width="60" align="center"></el-table-column>
        </el-table>
        
        <!-- 分页组件 -->
        <el-pagination 
          background 
          
          @size-change="handleSizeChange" 
          @current-change="handleCurrentChange" 
          :page-sizes="[5, 7, 10, 15]" 
          :current-page="queryForm.page" 
          :page-size="queryForm.limit" 
          :total="total" 
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 15px; text-align: right;"
        ></el-pagination>
    
    <!-- 弹窗组件 -->
    <meal-info  :isShow="dialogVisible"  :mealInfo="currentMeal"  @closeDialog="handleCloseDialog"  @mealSave="handleMealSave">
    </meal-info>
  </div>
</template>

<script>
import MealInfo from '@/components/MealInfo.vue'

export default {
  name: 'foodmanagement',
  components: { MealInfo },
  data() {
    return {
      mealList: [],
      checkedMealList: [], 
      queryForm: {
        mname: '',
        page: 1,
        limit: 7
      },
      total: 0,
      dialogVisible: false,
      currentMeal: {}
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.checkedMealList = val
    },

    getTableData() {
      this.$http.post("/selectMeals", this.queryForm).then(res => {
        this.mealList = res.data.data
        this.total = res.data.count
      })
    },

    handleSizeChange(val) {
      this.queryForm.limit = val
      this.getTableData()
    },

    handleCurrentChange(val) {
      this.queryForm.page = val
      this.getTableData()
    },

    handleAdd() {
      this.currentMeal = {
        mid: 0,
        mname: '',
        price: '',
        isavaliable: 1
      }
      this.dialogVisible = true
    },

    handleEdit() {
      if (this.checkedMealList.length === 0) {
        this.$message.warning("请选择要修改的餐品信息")
        return
      } else if (this.checkedMealList.length > 1) {
        this.$message.warning("只能选择一条餐品数据进行修改")
        return
      } else {
        let mid = this.checkedMealList[0].mid
      this.$http.get('/getOneMeal/' + mid).then(res => {
        this.currentMeal = res.data
        this.dialogVisible = true
      })
      }
 
    },

    handleDelete() {
      if(this.checkedMealList.length == 0){
        this.$message.warning("请选择要删除的数据")
        return
      }
      this.$confirm('确定要删除选中数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let mids = []
        for(let meal of this.checkedMealList){
          mids.push(meal.mid)
        }
        this.$http.post('/delBatchMeals', mids).then(res => {
          if(res.data === 'success'){
            this.$message.success("删除成功")
            this.getTableData()
          } else {
            this.$message.error("删除失败")
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },

    handleCloseDialog() {
      this.dialogVisible = false
      this.currentMeal = {}
    },
    
    handleMealSave() {
      this.dialogVisible = false
      this.$message.success("操作成功！")
      this.getTableData()
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style scoped>
.food-management-container {
  padding: 20px;
}

/* 让表格列均匀分布 */
::v-deep .el-table th {
  background-color: #f5f7fa;
}

/* 确保表格占满容器宽度 */
::v-deep .el-table {
  table-layout: fixed;
}

::v-deep .el-table__body {
  width: 100% !important;
}
</style>